<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class agentprice extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
		// Call the Model constructor
		parent::_construct();
	}
	

	function show($y,$m,$idServ)
	{
		
		$n=($m!=2?(($m%2)^($m>7))+30:(!($y%400)||!($y%4)&&($y%25)?29:28));
		if(strlen($m)<2)
		{
			$m='0'.$m;
		}
		$dateS=$y."-".$m."-01";
		$dateE=$y."-".$m."-".$n;

		$query="select idStruct from listoftables where idservice=".$idServ." group by idStruct";
		$result=$this->db->query($query);
		$rows2=array();
		foreach($result->result() as $row)
		{
			$query="Select idRow from liststruct where idstruct=".$row->idStruct;
			$res=$this->db->query($query);
			foreach($res->result() as $row2)
			{
				if(!isset($rows2[$row2->idRow]))$rows2[$row2->idRow]=$row2->idRow;
			}
		}
		$i=0;
		$responce->page = 1;
		$responce->total = 1;
		$responce->records = count($rows2);
		$rows=array();
		foreach($rows2 as $row)
		{
			$rows[$i]['cell']=array($row,$row);
			$query="select `date`,price,type from agentprice where userId=".$_SESSION['idUser']." and `date`>='".$dateS."' and idService=".$idServ." and `date`<='".$dateE."' and idRow=".$row;
			$this->log($query);
			$result=$this->db->query($query)->result_array();
			for($j=1;$j<=$n;$j++)
			{
				$f=-1;
				$date=$y.'-'.$m.'-';
				
				if(strlen ($j)<2)
				{
					$date.='0'.$j;
				}
				else
				{
					$date.=$j;
				}
				foreach($result as $key=>$r)
				{
					//$this->log($date.' - '.$r['date']);
					if($r['date']==$date)
					{
						$f=$key;
					}
				}
				if($f!=-1)
				{
					if($result[$f]['type']==1)
					{
					$rows[$i]['cell'][]=$result[$f]['price']."%";
					}
					else
					{
						$rows[$i]['cell'][]='+'.$result[$f]['price'];
					}
				}
				else {
					$rows[$i]['cell'][]="";
				}
			}
			$i++;
		}
		$responce->rows=$rows;
		return json_encode($responce);
	}
	function edit($idServ,$type,$days,$year,$month)
	{
		
		
		$date=$year.'-';
		if(strlen ($month)<2)
		{
			$date.='0'.$month.'-';
		}
		else
		{
			$date.=$month.'-';
		}
		
		foreach($days as $day=>$price)
		{
				$intDay=substr($day, 1);
				if($intDay<10)
				{
					$intDay='0'.$intDay;
				}
				
				$query="Select id from agentprice where date='".$date.$intDay."' and idService=".$idServ." and idRow=".$type." and userId=".$_SESSION['idUser'];
				$this->log($query);
				$result=$this->db->query($query);
				if($result->num_rows()>0)
				{
					$query="Update `agentprice` SET  `price` =  ".$price." WHERE `id` = ".$result->row()->id;
					
					$this->db->query($query);
				}
				else
				{
					if($price!=0)
					{
						$query="insert into agentprice (userId,idRow,`date`,`price`,idservice) values ('".$_SESSION['idUser']."','$type','".$date.$intDay."','".$price."','".$idServ."')";
						$this->log($query);
						$this->db->query($query);
						$this->db->close();
					}
				}
		}
	}
function setprice($idserv,$dates,$rows,$skidka)
	{
		
		$date=explode(';',$dates);
		
			foreach($date as $day)
			{
	
				if((preg_match("/^(\d\d\d\d)-(\d|\d\d)-(\d|\d\d)/",$day)))
				{
					$dayA=explode('-',$day);
					$day=$dayA[0]."-";
					$day.=strlen($dayA[1])<2?'0'.$dayA[1].'-':$dayA[1].'-';
					$day.=strlen($dayA[2])<2?'0'.$dayA[2]:$dayA[2];
					
					foreach($rows as $idRow => $price)
					{
						if($price!=0)
						{
							$query="Select id from agentprice where idservice=".$idserv." and idRow=".$idRow." and `date`='".$day."' and userId=".$_SESSION['idUser'];
							$result=$this->db->query($query);
							if($result->num_rows>0)
							{
								$query="update agentprice set price=".$price.", type=".$skidka." where id=".$result->row()->id;
								$this->db->query($query);
							}
							else
							{
								$query="insert into agentprice (`idservice`,`idrow`,`date`,`price`,type,userId) values ($idserv,$idRow,'$day',$price,$skidka,".$_SESSION['idUser'].")";
								$this->db->query($query);
							}
						}
					}
				}
			}
		}
		function deleteTableDataByMonth ($idserv,$date)
		{
			$query = "delete from agentprice where idservice=".$idserv." and left(Date,7)='".$date."' and userId=".$_SESSION['idUser'];
			$this->Log("Удаление по запросу-----".$query);
			$result = $this->db->query($query);
			if ($result)
			$this->Log("Удаление прошло успешно-----".$query);
			else
			$this->Log("Удаление провалилось-----".$query);
		}	
		function deleteTableData ($idServ)
		{
			$query = "delete from agentprice where idservice=".$idServ." and userId=".$_SESSION['idUser'];
			$this->Log("Удаление по запросу-----".$query);
			$result = $this->db->query($query);
			if ($result)
			$this->Log("Удаление прошло успешно-----".$query);
			else
			$this->Log("Удаление провалилось-----".$query);
		}
}